package mx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import mx.entity.Advert;
import mx.entity.PageBean;
import mx.service.AdvertService;
import mx.utils.DateJsonValueProcessor;
import mx.utils.ResponseUtil;
import mx.utils.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/advert")
public class AdvertController {

	@Resource
	private AdvertService advertService;

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param rows
	 * @param response
	 * @param toilet
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			HttpServletResponse response, Advert advert) throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page),
				Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("account", StringUtil.formatLike(advert.getAccount()));
		map.put("type", advert.getType());
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Advert> ToiletList = advertService.list(map);
		long totatl = advertService.getTotal(map);
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(ToiletList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", totatl);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 条件删除
	 * 
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(
			@RequestParam(value = "ids", required = false) String ids,
			HttpServletResponse response) throws Exception {
		String[] id = ids.split(",");
		JSONObject result = new JSONObject();
		for (String Advert : id) {
			advertService.delete(Integer.parseInt(Advert));
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 添加或更新厕所
	 * 
	 * @param toilet
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String addOrUpdate(Advert advert, HttpServletResponse response)
			throws Exception {
		int resultTotal = 0;
		JSONObject result = new JSONObject();
		if (advert.getId() == null) {
			resultTotal = advertService.add(advert);// 添加厕所
		} else {
			resultTotal = advertService.update(advert);// 更新厕所
		}
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
}
